import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
// vuex持久化插件 会自动帮把state里的数据存进本地存储中
import vuexPersist from "vuex-persist";

export default new Vuex.Store({
  state: {
    list: [],
    copylist:[]
  },
  mutations: {
    add(state, i) {
      console.log(i);
      var flag = false;
      state.copylist.forEach((item) => {
        if (item.id == i) {
          flag = true;
          item.num++;
        }
      });
      if (flag == false) {
        state.list.push(i);
      }
      // state.list.push(i);
    },
    set(state, b) {
      state.copylist = b;
    }
    // setlist(state, val) {
    //   if (localStorage.getItem("vuex")) {
    //     // console.log(1);
    //     state.list = JSON.parse(localStorage.getItem("vuex"))
    //   } else {
    //     // console.log(2);
    //     state.list = val
    //   }
    // }
  },
  actions: {
    // getData(context){
    //   this.$API.getStoreList().then((res)=>{
    //     context.commit("setlist",res.data.data);

    //   })
    // }
  },
  modules: {
  },
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
})
